layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //学校列表
    var tableIns = table.render({
        elem: '#paramPhotoList',
        url : '/paramPhotoList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [5,15,20,25],
        limit : 5,
        id : "paramPhotoListTable",
        cols : [[
        {type: "checkbox", fixed:"left", width:50},
        {type: "numbers", title: '序号',fixed:"left", width:50},
        {field: 'xxmc', title: '学校名称', minWidth:100, align:"center"},
        {field: 'spidStr', title: '照片规格', minWidth:150, align:'center'},
        {field: 'bkColorStr', title: '背景色', minWidth:60, align:'center'},
        {title: '操作', minWidth:190,fixed:"right",align:"center",templet:"#paramPhotoListBar"}
        ]],
        done: function () {
            $("[data-field='id']").css('display','none');
        }
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("paramPhotoListTable",{
                url:"/paramPhotoList",
                page: {
                    page: 1 //重新从第 1 页开始
                },
                where: {
                    searchVal: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加
    function addParam(edit){
        var title = "调整照片默认参数";
        var url = "/toPhotoParamAdd";
        var index = layui.layer.open({
            title : title,
            type : 2,
            content : url,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find("#xxId").val(edit.xxId);
                    body.find(".xxmc").val(edit.xxmc);
                    body.find("#paramId").val(edit.paramId);
                    body.find(".spidList").val(edit.spid);
                    body.find(".bkColorList").val(edit.bkColor);
                    form.render();
                }else{
                    body.find("#xxDiv").css('display', 'none');
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
    $(".add_demo_btn").click(function(){
        addParam();
    });

    //批量设置
    $(".pl_set_btn").click(function(){
        var checkStatus = table.checkStatus('paramPhotoListTable'),
            data = checkStatus.data,
            xxIds = "",
            xxmcs = "";
        if(data.length > 0) {
            for (var i in data) {
                if(i==data.length-1){
                    xxIds+=data[i].xxId;
                    xxmcs+=data[i].xxmc;
                }else{
                    xxIds+=data[i].xxId+",";
                    xxmcs+=data[i].xxmc+",";
                }
            }
            layer.confirm('确定批量设置选中的学校？', {icon: 3, title: '提示信息'}, function (index) {
                var PhotoParam={
                    xxId:xxIds,
                    xxmc:xxmcs,
                    spid:"1",
                    bkColor:"blue"
                };
                addParam(PhotoParam);
                layer.close(index);
            })
        }else{
            layer.msg("请选择需要设置学校");
        }
    });

    //批量默认
    $(".pl_reset_btn").click(function(){
        var checkStatus = table.checkStatus('paramPhotoListTable'),
            data = checkStatus.data,
            xxIds = "",
            xxmcs = "";
        if(data.length > 0) {
            for (var i in data) {
                if(i==data.length-1){
                    xxIds+=data[i].xxId;
                    xxmcs+=data[i].xxmc;
                }else{
                    xxIds+=data[i].xxId+",";
                    xxmcs+=data[i].xxmc+",";
                }
            }
            layer.confirm('确定批量设置选中的学校,设置成默认值(一寸/蓝底)？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("/resetPhotoParam",{
                    xxId : xxIds //参数传入
                },function(data){
                    if (data =="success"||data.includes("success")) {
                        tableIns.reload();
                        layer.close(index);
                    }else if (data == "error") {
                        layer.msg("设置失败！");
                    }

                });
                layer.close(index);
            })
        }else{
            layer.msg("请选择需要设置学校");
        }
    });

    //列表操作
    table.on('tool(paramPhotoList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addParam(data);
        }else if(layEvent === 'reset'){ //默认
            layer.confirm('确定设置成默认值(一寸/蓝底)？',{icon:3, title:'提示信息'},function(index){
                $.get("/resetPhotoParam",{
                    xxId : data.xxId //参数传入
                },function(data){
                    if (data =="success") {
                        tableIns.reload();
                        layer.close(index);
                    }else if (data == "error") {
                        layer.msg("设置失败！");
                    }
                })
            });
        }else if(layEvent === 'extra'){ //额外新增
            var dataTemp = {
              xxId: data.xxId
            };
            addParam(dataTemp);
        }else if(layEvent === 'delete'){ //删除
            //TODO 删除，但是不能少于一条
        }
    });
    document.onkeydown = function(e){
        var ev = document.all ? window.event : e;
        if(ev.keyCode==13) {
            table.reload("paramPhotoListTable",{
                url:"/paramPhotoList",
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    searchVal: $(".searchVal").val()  //搜索的关键字
                }
            })
        }
    }
})
